using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ERPBackend.Migrations
{
    /// <inheritdoc />
    public partial class FixPurchaseStockInSupplierForeignKey : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 尝试删除外键约束，如果它存在
            migrationBuilder.Sql("IF EXISTS (SELECT * FROM sys.foreign_keys WHERE name = 'FK_purchase_stock_in_supplier_supplier_id')\n" +
                                  "ALTER TABLE purchase_stock_in DROP CONSTRAINT FK_purchase_stock_in_supplier_supplier_id");

            // 删除相关索引，如果它存在
            migrationBuilder.Sql("IF EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_purchase_stock_in_supplier_id')\n" +
                                  "DROP INDEX IX_purchase_stock_in_supplier_id ON purchase_stock_in");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // 在回滚时重新创建外键约束和索引
            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_supplier_id",
                table: "purchase_stock_in",
                column: "supplier_id");

            migrationBuilder.AddForeignKey(
                name: "FK_purchase_stock_in_supplier_supplier_id",
                table: "purchase_stock_in",
                column: "supplier_id",
                principalTable: "supplier",
                principalColumn: "id");
        }
    }
}
